package com.ruoyi.web.controller.store;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fitment.domain.StoreFitment;
import com.ruoyi.fitment.service.IStoreFitmentService;
import com.ruoyi.store.domain.Store;
import com.ruoyi.common.core.domain.entity.StoreUserRole;
import com.ruoyi.store.service.IStoreService;
import com.ruoyi.store.service.IStoreUserRoleService;
import com.ruoyi.wechat.service.WeChatMessageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.store.domain.StoreApply;
import com.ruoyi.store.service.IStoreApplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 门店申请Controller
 *
 * @author ruoyi
 * @date 2021-11-17
 */
@RestController
@RequestMapping("/admin/store/storeApply")
public class StoreApplyController extends BaseController
{
    @Autowired
    private IStoreApplyService storeApplyService;

    @Autowired
    private IStoreService storeService;
    @Autowired
    private IStoreUserRoleService roleService;
    @Autowired
    private WeChatMessageService messageService;
    @Autowired
    private IStoreFitmentService fitmentService;
    /**
     * 查询门店申请列表
     */
    @PreAuthorize("@ss.hasPermi('store:store:list')")
    @GetMapping("/list")
    public TableDataInfo list(StoreApply storeApply)
    {
        startPage();
        List<StoreApply> list = storeApplyService.selectStoreApplyList(storeApply);
        return getDataTable(list);
    }

    /**
     * 导出门店申请列表
     */
    @PreAuthorize("@ss.hasPermi('store:store:export')")
    @Log(title = "门店申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StoreApply storeApply)
    {
        List<StoreApply> list = storeApplyService.selectStoreApplyList(storeApply);
        ExcelUtil<StoreApply> util = new ExcelUtil<StoreApply>(StoreApply.class);
        return util.exportExcel(list, "门店申请数据");
    }

    /**
     * 获取门店申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:store:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(storeApplyService.selectStoreApplyById(id));
    }
    /**
     * 修改门店申请
     */
    @PreAuthorize("@ss.hasPermi('store:store:edit')")
    @Log(title = "门店申请通过", businessType = BusinessType.UPDATE)
    @PutMapping("/pass/{ids}")
    public AjaxResult pass(@PathVariable Long[] ids)
    {
        for(int i = 0; i < ids.length; i++){
            StoreApply apply = storeApplyService.selectStoreApplyById(ids[i]);
            Store store = new Store(apply);
            store.setShowState(DicConstants.OPERATION_STATE_IN_Business.getCode());
            store.setStoreState(DicConstants.STORE_STATE_Open.getCode());
            System.out.println("tangyin000");
            System.out.println(apply);
            System.out.println(store);
            if(apply.getStoreId()!=null){
                storeService.updateStore(store);
            }else{

                storeService.insertStore(store);
                StoreFitment fitment = new StoreFitment();
                fitment.setPicUrl(store.getStoreBacImage());
                fitment.setStoreId(store.getId());
                fitment.setFitmentType(DicConstants.FITMENT_TYPE_SINGLE.getCode());
                fitmentService.insertStoreFitment(fitment);
                StoreUserRole role = new StoreUserRole();
                role.setRoleType(DicConstants.STORE_Identity_Manager.getCode());
                role.setStoreUserId(apply.getCreateBy());
                role.setStoreId(store.getId());
                role.setCreateBy(getUserId());
                role.setCreateType(DicConstants.BUSINESS_Manager.getCode());
                roleService.insertStoreUserRole(role);
            }
            apply.setApplyState(DicConstants.AUIT_STATE_PASS.getCode());
            apply.setUpdateBy(getUserId());
            apply.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
            apply.setStoreId(store.getId());
            storeApplyService.updateStoreApply(apply);
            messageService.tipMsg(apply.getCreateBy(),apply);

        }
        return toAjax(1);
    }
    /**
     * 拒绝门店申请
     */
    @PreAuthorize("@ss.hasPermi('store:store:edit')")
    @Log(title = "拒绝", businessType = BusinessType.UPDATE)
    @PutMapping("/refuse")
    public AjaxResult refuse(@RequestBody StoreApply storeApply)
    {
        storeApply.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        storeApply.setUpdateBy(getUserId());
        storeApply.setApplyState(DicConstants.AUIT_STATE_REFUSE.getCode());
        int i = storeApplyService.updateStoreApply(storeApply);
        messageService.refuseMsg(storeApply.getCreateBy(),storeApply);
        return toAjax(i);
    }

    /**
     * 删除门店申请
     */
    @PreAuthorize("@ss.hasPermi('store:store:remove')")
    @Log(title = "门店申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(storeApplyService.logicDeleteStoreApply(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
